package cn.iocoder.yudao.module.system.controller.admin.monitor;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.monitor.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.monitor.MonitorDO;
import cn.iocoder.yudao.module.system.service.monitor.MonitorService;

@Tag(name = "管理后台 - 监控内容")
@RestController
@RequestMapping("/system/monitor")
@Validated
public class MonitorController {

    @Resource
    private MonitorService monitorService;

    @PostMapping("/create")
    @Operation(summary = "创建监控内容")
    @PreAuthorize("@ss.hasPermission('system:monitor:create')")
    public CommonResult<Long> createMonitor(@Valid @RequestBody MonitorSaveReqVO createReqVO) {
        return success(monitorService.createMonitor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新监控内容")
    @PreAuthorize("@ss.hasPermission('system:monitor:update')")
    public CommonResult<Boolean> updateMonitor(@Valid @RequestBody MonitorSaveReqVO updateReqVO) {
        monitorService.updateMonitor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除监控内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:monitor:delete')")
    public CommonResult<Boolean> deleteMonitor(@RequestParam("id") Long id) {
        monitorService.deleteMonitor(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除监控内容")
                @PreAuthorize("@ss.hasPermission('system:monitor:delete')")
    public CommonResult<Boolean> deleteMonitorList(@RequestParam("ids") List<Long> ids) {
        monitorService.deleteMonitorListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得监控内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:monitor:query')")
    public CommonResult<MonitorRespVO> getMonitor(@RequestParam("id") Long id) {
        MonitorDO monitor = monitorService.getMonitor(id);
        return success(BeanUtils.toBean(monitor, MonitorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得监控内容分页")
    @PreAuthorize("@ss.hasPermission('system:monitor:query')")
    public CommonResult<PageResult<MonitorRespVO>> getMonitorPage(@Valid MonitorPageReqVO pageReqVO) {
        PageResult<MonitorDO> pageResult = monitorService.getMonitorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MonitorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出监控内容 Excel")
    @PreAuthorize("@ss.hasPermission('system:monitor:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMonitorExcel(@Valid MonitorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MonitorDO> list = monitorService.getMonitorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "监控内容.xls", "数据", MonitorRespVO.class,
                        BeanUtils.toBean(list, MonitorRespVO.class));
    }

}